package com.ruoyi.project.mall.controller;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.mall.domain.MallCompanyUserDO;
import com.ruoyi.project.mall.service.MallCompanyUserService;
import com.ruoyi.project.wx.domain.MemberUserDO;
import com.ruoyi.project.wx.service.MemberUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


/**
 * 企业用户
 * 
 * @author long
 * @email 499626561@qq.com
 * @date 2020-12-25 10:18:47
 */
 
@Controller
@RequestMapping("/mall/mallCompanyUser")
public class MallCompanyUserController extends BaseController {
	@Autowired
	private MallCompanyUserService mallCompanyUserService;
	@Autowired
	private MemberUserService memberUserService;
	
	/*@GetMapping()
	@RequiresPermissions("mall:mallCompanyUser:mallCompanyUser")
	String MallCompanyUser(){
	    return "mall/mallCompanyUser/mallCompanyUser";
	}*/
	

	@PreAuthorize("@ss.hasPermi('mall:mallCompanyUser:mallCompanyUser')")
	@GetMapping("/list")
	@ResponseBody
	public TableDataInfo list(MallCompanyUserDO mallCompanyUser){

		{
			startPage();
			Map<String, Object> stringObjectMap = BeanUtil.beanToMap(mallCompanyUser);
			List<MallCompanyUserDO> mallCompanyUserList = mallCompanyUserService.likeList(stringObjectMap);
			if(mallCompanyUserList.size()>0){
				for (MallCompanyUserDO mallCompanyUserDO : mallCompanyUserList) {
					MemberUserDO memberUserDO1 = memberUserService.get(mallCompanyUserDO.getUserId());
					if(memberUserDO1!=null){
						mallCompanyUserDO.setUserId(memberUserDO1.getName());
					}else{
						mallCompanyUserDO.setUserId("---");
					}
					MallCompanyUserDO mallCompanyUserDO1 = mallCompanyUserService.get(mallCompanyUserDO.getParentId());
					if(mallCompanyUserDO1!=null){
						MemberUserDO memberUserDO = memberUserService.get(mallCompanyUserDO1.getUserId());
						if(memberUserDO!=null){
							mallCompanyUserDO.setParentId(memberUserDO.getName());
						}else{
							mallCompanyUserDO.setParentId("----");
						}

					}

				}
			}
			return getDataTable(mallCompanyUserList);
		}
	}

	/**
	 * 导出企业用户列表
	 */
	@ResponseBody
	@PreAuthorize("@ss.hasPermi('mall:mallCompanyUser:export')")
	@Log(title = "企业用户", businessType = BusinessType.EXPORT)
	@GetMapping("/export")
	public AjaxResult export(MallCompanyUserDO mallCompanyUser)
	{
		Map<String, Object> stringObjectMap = BeanUtil.beanToMap(mallCompanyUser);
		List<MallCompanyUserDO> list = mallCompanyUserService.list(stringObjectMap);
		ExcelUtil<MallCompanyUserDO> util = new ExcelUtil<MallCompanyUserDO>(MallCompanyUserDO.class);
		return util.exportExcel(list, "mallCompanyUser");
	}

	/**
	 * 获取企业用户详细信息
	 */
	@ResponseBody
	@PreAuthorize("@ss.hasPermi('mall:mallCompanyUser:query')")
	@GetMapping(value = "/{id}")
	public AjaxResult getInfo(@PathVariable("id") String id)
	{

		return AjaxResult.success(mallCompanyUserService.get(id));
	}


	/**
	 * 新增企业用户
	 */
	@ResponseBody
	@PreAuthorize("@ss.hasPermi('mall:mallCompanyUser:add')")
	@Log(title = "企业用户", businessType = BusinessType.INSERT)
	@PostMapping
	public AjaxResult add(@RequestBody MallCompanyUserDO mallCompanyUser)
	{
		return toAjax(mallCompanyUserService.save(mallCompanyUser));
	}

	/**
	 * 修改企业用户
	 */
	@ResponseBody
	@PreAuthorize("@ss.hasPermi('mall:mallCompanyUser:edit')")
	@Log(title = "企业用户", businessType = BusinessType.UPDATE)
	@PutMapping
	public AjaxResult edit(@RequestBody MallCompanyUserDO mallCompanyUser)
	{
		return toAjax(mallCompanyUserService.update(mallCompanyUser));
	}

	/**
	 * 删除企业用户
	 */
	@ResponseBody
	@PreAuthorize("@ss.hasPermi('mall:mallCompanyUser:remove')")
	@Log(title = "企业用户", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
	public AjaxResult remove(@PathVariable String[] ids)
	{
		return toAjax(mallCompanyUserService.batchRemove(ids));
	}


	/*@GetMapping("/add")
	@RequiresPermissions("mall:mallCompanyUser:add")
	String add(){
	    return "mall/mallCompanyUser/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("mall:mallCompanyUser:edit")
	String edit(@PathVariable("id") String id,Model model){
		MallCompanyUserDO mallCompanyUser = mallCompanyUserService.get(id);
		model.addAttribute("mallCompanyUser", mallCompanyUser);
	    return "mall/mallCompanyUser/edit";
	}
	
	*//**
	 * 保存
	 *//*
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("mall:mallCompanyUser:add")
	public R save(MallCompanyUserDO mallCompanyUser){
	    mallCompanyUser.setId(UuidUtil.get32UUID());
	    mallCompanyUser.setDelFlag(0);
	    mallCompanyUser.setCaAt(new Date());
		if(mallCompanyUserService.save(mallCompanyUser)>0){
			return R.ok();
		}
		return R.error();
	}
	*//**
	 * 修改
	 *//*
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("mall:mallCompanyUser:edit")
	public R update( MallCompanyUserDO mallCompanyUser){
		mallCompanyUser.setUpAt(new Date());
		mallCompanyUserService.update(mallCompanyUser);
		return R.ok();
	}
	
	*//**
	 * 删除
	 *//*
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("mall:mallCompanyUser:remove")
	public R remove( String id){
		MallCompanyUserDO mallCompanyUser = mallCompanyUserService.get(id);
		Map<String,Object> map = new HashMap<>();
		map.put("delFlag","0");
		map.put("parentId",mallCompanyUser.getId());

		List<MallCompanyUserDO> list = mallCompanyUserService.list(map);
		if(list.size()>0){
			return R.error("请先删除下级");
		}
		mallCompanyUser.setDelFlag(1);
		mallCompanyUser.setUpAt(new Date());
		if(mallCompanyUserService.update(mallCompanyUser)>0){
		return R.ok();
		}
		return R.error();
	}
	
	*//**
	 * 删除
	 *//*
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("mall:mallCompanyUser:batchRemove")
	public R remove(@RequestParam("ids[]") String[] ids){
	   if(ids.length>0){
			for (String string : ids) {
				MallCompanyUserDO mallCompanyUser = mallCompanyUserService.get(string);
				mallCompanyUser.setDelFlag(1);
				mallCompanyUser.setUpAt(new Date());
				mallCompanyUserService.update(mallCompanyUser);
			}
		}
	
		return R.ok();
	}*/
	
}
